package com.tenglong.teacher.ui.downApk.iView;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.content.FileProvider;

import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.cuotibao.zhuoxiao.basics.utils.permission.PermissionManager;
import com.tenglong.teacher.BuildConfig;
import com.tenglong.teacher.data.resultInfo.CurrentVersionInfo;
import com.tenglong.teacher.ui.dialog.UpdateVersionDialog;
import com.tenglong.teacher.ui.dialog.VersionDialog;
import com.tenglong.teacher.ui.downApk.IContract;
import com.tenglong.teacher.ui.downApk.iPresenter.ServicePresenterImpl;

import java.io.File;

/**
 * @author hdx on 2019/2/26.
 */
public class ViewPresenter implements IContract.View {

    private final ServicePresenterImpl mPresenter;
    private Activity mActivity;
    private UpdateVersionDialog mProgressBar;
    private VersionDialog mVersionInfoDialog;
    public static int DOWN_LOAD_STATUS = 0;//0代表未执行下载任务，1正在执行下载任务

    public ViewPresenter(Activity activity) {
        this.mActivity = activity;
        mPresenter = new ServicePresenterImpl(this);
    }

    @Override
    public void yetNewsVersion() {
//        ToastUtils.showCenterToast(mActivity,"最新版本");
    }

    @Override
    public void showUpdate(final CurrentVersionInfo message, VersionDialog.VersionDialogInterface versionDialogInterface) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            if (mActivity.isDestroyed()) {
                return;
            }
        }
        if (mActivity.isFinishing()) {
            return;
        }
        mVersionInfoDialog = new VersionDialog(mActivity);
        mVersionInfoDialog.setOnDialogListener(versionDialogInterface);
        mVersionInfoDialog.showDialog(message.versionName);
    }

    @Override
    public void showProgress(int progress) {
        if (mProgressBar == null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                if (mActivity.isDestroyed()) {
                    return;
                }
            }
            if (mActivity.isFinishing()) {
                return;
            }
            mProgressBar = new UpdateVersionDialog(mActivity);
            mProgressBar.showDialog();
        }
        mProgressBar.updateProgressBar(progress);
    }

    @Override
    public void showFail(String msg) {
        ToastUtils.showCenterToast(msg);
    }

    @Override
    public void showComplete() {
        if (mProgressBar != null) {
            mProgressBar.closeDialog();
        }
    }

    @Override
    public Intent install(File file) {
        return getIntent(file);
    }

    @Override
    public Context getAppContext() {
        return mActivity;
    }

    @Override
    public void onDestroy() {
        mPresenter.unbind(mActivity);
    }

    @Override
    public void checkUpdate(int oldVersion, IContract.CheckUpdateListener listener) {
        mPresenter.checkUpdate(oldVersion, listener);
    }

    @Override
    public void downLoad() {
        PermissionManager.getInstances().requestPermission(1, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new Runnable() {
            @Override
            public void run() {
                mPresenter.downApk(mActivity);
            }
        }, null, mActivity);
    }

    @NonNull
    private Intent getIntent(File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 24) { //判读版本是否在7.0以上
            Uri apkUri =
                    FileProvider.getUriForFile(mActivity, BuildConfig.APPLICATION_ID + ".fileprovider", file);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(file),
                    "application/vnd.android.package-archive");
        }
        return intent;
    }
}
